package com.raymond.cmpp.client.handler;

import com.raymond.cmpp.common.msg.request.ActiveTestRequestMessage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 心跳触发器
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-08-06 11:39
 */
public class ClientIdleStateTrigger extends ChannelInboundHandlerAdapter {
    private final static Logger logger = LoggerFactory.getLogger(ClientIdleStateTrigger.class);

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleState state = ((IdleStateEvent) evt).state();
            if (state == IdleState.ALL_IDLE) {
                logger.debug("发送心跳");
                ctx.writeAndFlush(new ActiveTestRequestMessage());
            }
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }
}
